import Eos from "./platform/EosUtil";
import Constants from "./platform/Constants";
import AccountInfo from "./EosAccountInfo";
import HttpClient from "./HttpClient";
import ChainInfo from "./EosChainInfo";

const init = (eosServer) => {

};

const seedPrivateKey = (payload,callback)=>{
    //公钥生成私钥
    Eos.seedPrivateKey(payload.ownerSeed,payload.activeSeed,(resp)=>{
        if(resp){
            console.log('seedPrivateKey ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    });
};

const randomPrivateKey = (callback)=>{
    //随机生成私钥
    Eos.randomPrivateKey((resp)=>{
        if(resp){
            console.log('randomPrivateKey ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    });
};

const privateToPublic = (privateKey,callback) =>{
    //私钥生成公钥
    Eos.privateToPublic(privateKey,(resp)=>{
        if(resp){
            console.log('privateToPublic ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const sign = (data,pk,callback) => {
    //签名数据块
    Eos.sign(data,pk,(resp)=>{
        if(resp){
            console.log('sign ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    });
};

const transfer = (contract,from,to,quantity,memo,pk,broadcast,callback) =>{
    //交易
    Eos.transfer(contract,from,to,quantity,memo,pk,broadcast,(resp)=>{
        if(resp){
            console.log('transfer ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};


const createAccount = (creator,createPrivateKey,newAccount,onwerPublicKey,activePublicKey,callback) => {
    //创建账号
    Eos.createAccount(creator,createPrivateKey,newAccount,onwerPublicKey,activePublicKey,(resp)=>{
        if(resp){
            console.log('createAccount ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    });
};

const balance = (contract,account,callback) =>{
    //余额
    Eos.balance(contract,account,(resp)=>{
        if(resp){
            console.log('balance ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const checkPublicKey = (publickey,callback) =>{
    //检测公钥有效
    Eos.checkPublicKey(publickey,(resp)=>{
        if(resp){
            console.log('checkPublicKey ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const checkPrivateKey = (privatekey,callback) =>{
    //检测私钥有效
    Eos.checkPrivateKey(privatekey,(resp)=>{
        if(resp){
            console.log('checkPrivateKey ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const delegate = (pk,pay,recive,cpu,net,callback) =>{
    //质押宽带
    Eos.delegate(pk,pay,recive,cpu,net,(resp)=>{
        if(resp){
            console.log('delegate ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const undelegate = (pk,from,recive,cpu,net,callback) =>{
    //赎回宽带
    Eos.undelegate(pk,from,recive,cpu,net,(resp)=>{
        if(resp){
            console.log('undelegate ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const buyram = (pk,pay,recive,count,callback) =>{
    //购买内存
    Eos.buyram(pk,pay,recive,count,(resp)=>{
        if(resp){
            console.log('buyram ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const sellram = (pk,recive,bytes,callback) =>{
    //出售内存
    Eos.sellram(pk,recive,bytes,(resp)=>{
        if(resp){
            console.log('sellram ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const vote = (actor,voter,producers,privateKey,callback)=>{
    //投票
    Eos.vote(actor,voter,producers,privateKey,(resp)=>{
        if(resp){
            console.log('vote ' + JSON.stringify(resp.data));
            if(callback){callback(resp.data)}
        }
    })
};

const unvote = (actor,voter,producters,privateKey,callback)=>{
    //撤票

};

const delegatebw = (actor,from,receiver,net,cpu,privateKey,callback)=>{
    // 抵押

};

const undelegatebw = (actor,from,receiver,net,cpu,privateKey,callback)=>{
    // 赎回

};

const existAccount = (accountName,callback,cache=false)=>{
    //根据账户名获取账号
    let req = {
        account_name:accountName
    };
    let url = Constants.EOSURL+'/chain/get_account';
    HttpClient.request(url,'post',req,cache,(data)=>{
        if(data && data.account_name){
            if(callback){callback({exist:true})}
            console.log('existAccount exist true');
        }else{
            if(callback){callback({exist:false})}
            console.log('existAccount exist true');
        }
    });
};

const getAccount = (accountName,callback,cache=true)=>{
    //根据账户名获取账号
    let req = {
        account_name:accountName
    };
    let url = Constants.EOSURL+'/chain/get_account';
    HttpClient.request(url,'post',req,cache,(data)=>{
        let input = {data:data};
        let account = AccountInfo.Format(input);
        //console.log('AccountInfo ' + JSON.stringify(account));
        if(callback){callback(account)}
    });
};

const getInfo = (callback,cache=true) => {
    //获取eos共链信息
    let req = {};
    let url = Constants.EOSURL+'/chain/get_info';
    HttpClient.request(url,'post',req,cache,(data)=>{
        let format = ChainInfo.FormatInfo(data);
        console.log('getInfo format:' + JSON.stringify(format));
        if(callback){callback(format)}
    });
};

const getGlobalInfo = (callback,cache=true) => {
    //获取全网信息
    let req = {
        "json": true,
        "code": "eosio",
        "scope": "eosio",
        "table": "global",
        "limit": 10,
        "lower_bound": 0,
        "uppper_bound": -1
    };
    let url = Constants.EOSURL+'/chain/get_table_rows';
    HttpClient.request(url,'post',req,cache,(data)=>{
        let format = ChainInfo.FormatGlobal(data);
        console.log('getGlobalInfo format:' + JSON.stringify(format));
        if(callback){callback(format)}
    });
};

const getBlock = (blockId,callback,cache=true) => {
    //获取某个区块数据
    let req = {
        block_num_or_id:blockId
    };
    let url = Constants.EOSURL+'/chain/get_block';
    HttpClient.request(url,'post',req,cache,(data)=>{
        if(callback){callback(data)}
    });
};

const getProducers = (payload,callback,cache=true) => {
    //获取超级节点
    let req = {
        limit:payload.limit,
        lower_bound:payload.lowerBound,
        json:true,
    };
    let url = Constants.EOSURL+'/chain/get_producers';
    HttpClient.request(url,'post',req,cache,(data)=>{
        if(callback){callback(data)}
    });
};

const getCurrencyBalance = (payload,callback,cache=true) =>{
    //获取代币余额
    let req = {
        code:payload.code,
        account:payload.account,
        symbol:payload.symbol,
    };
    let url = Constants.EOSURL+'/chain/get_currency_balance';
    HttpClient.request(url,'post',req,cache,(data)=>{
        if(callback){callback(data)}
    });
};

const getKeyAccounts = (publicKey,callback,cache=true) => {
    //获取公钥下的账号
    let req = {
        public_key:publicKey,
    };
    let url = Constants.EOSURL+'/history/get_key_accounts';
    HttpClient.request(url,'post',req,cache,(resp)=>{
        if(!resp || !resp.account_names){
            if(callback){callback([])}
            return;
        }
        let accountNames = resp.account_names;
        if(!accountNames){
            accountNames = [];
        }
        let newAccounts = [];
        for(let i=0; i<accountNames.length; i++){
            getAccount(accountNames[i],(data)=>{
                newAccounts.push({name:accountNames[i],balance:data.balance.liquid, publicKey:publicKey});
                if(newAccounts.length === accountNames.length){
                    //console.log('getKeyAccounts ' + JSON.stringify(newAccounts));
                    if(callback){callback(newAccounts)}
                }
            });
        }
    });
};

const getActions = (payload,callback,cache = false) => {
    let req = {
        pos:payload.pos,
        offset:payload.offset,
        account_name:payload.accountName,
    };
    let url = Constants.EOSURL+'/history/get_actions';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const getTransaction = (payload,callback,cache = false) => {
    let req = {
        id:payload.id,
    };
    let url = Constants.EOSURL+'/history/get_transaction';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const getTransactions = (payload,callback,cache = false) => {
    let req = {
        id:payload.id,
    };
    let url = Constants.EOSURL+'/history/get_transaction';
    HttpClient.request(url,'post',req, cache, (data)=>{
        if(callback){callback(data)}
    });
};

const getRamPrice = (callback,cache=true) => {
    //获取内存价格
    let price = 0.15;
    if(callback){callback(price)}
};

const getCPUPrice = (callback,cache=true) => {
    //获取cpu价格
    let price = 0.15;
    if(callback){callback(price)}
};

const getNetPrice = (callback,cache=true) => {
    //获取net价格
    let price = 0.15;
    if(callback){callback(price)}
};

export default {
    init,
    seedPrivateKey,
    randomPrivateKey,
    privateToPublic,
    sign,
    transfer,
    balance,
    checkPublicKey,
    checkPrivateKey,
    delegate,
    undelegate,
    buyram,
    sellram,
    vote,
    unvote,
    delegatebw,
    undelegatebw,
    existAccount,
    getAccount,
    getInfo,
    getBlock,
    getGlobalInfo,
    getKeyAccounts,
    getProducers,
    getCurrencyBalance,
    getActions,
    getTransaction,
    getRamPrice
};